package com.ztao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.admin.dto.NewsAuthDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.wemedia.pojos.WmNews;
import com.heima.model.common.wemedia.pojos.WmUser;
import com.ztao.mapper.WmNewsMapper;
import com.ztao.mapper.WmUserMapper;
import com.ztao.service.NewsAuthService;
import com.ztao.service.WmNewsTaskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class NewsAuthServiceImpl implements NewsAuthService {
    @Resource
    private WmNewsMapper newsMapper;
    @Resource
    private WmUserMapper wmUserMapper;
//    @Override
//    public ResponseResult findAll_news(NewsAuthDto dto) {
//        dto.checkParam();
//        IPage<WmNews> page=new Page<>(dto.getPage(),dto.getSize());
//        QueryWrapper<WmNews> queryWrapper=new QueryWrapper<>();
//        if(dto.getStatus()!=null){
//            queryWrapper.lambda().like(WmNews::getStatus,dto.getStatus());
//        }
//       if(dto.getTitle()!=null){
//           queryWrapper.lambda().like(WmNews::getTitle,dto.getTitle());
//       }
//        queryWrapper.lambda().orderByDesc(WmNews::getCreatedTime);
//        page=newsMapper.selectPage(page,queryWrapper);
//        ResponseResult  result = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
//        result.setData(page.getRecords());
//        return result;
//    }
//媒体审核
    @Override
public ResponseResult findAll_news(NewsAuthDto dto) {
    dto.checkParam();
    IPage<WmNews> page=new Page<>(dto.getPage(),dto.getSize());
    QueryWrapper<WmNews> queryWrapper=new QueryWrapper<>();

    if(dto.getStatus()!=null){
        queryWrapper.lambda().like(WmNews::getStatus,dto.getStatus());
    }
    if(dto.getTitle()!=null){
        queryWrapper.lambda().like(WmNews::getTitle,dto.getTitle());
    }
    queryWrapper.lambda().orderByDesc(WmNews::getCreatedTime);
    page=newsMapper.selectPage(page,queryWrapper);
    for (WmNews record : page.getRecords()) {
        WmUser wmUser = wmUserMapper.selectById(record.getUserId());
        record.setAuthorName(wmUser.getName());
    }
    ResponseResult  result = new PageResponseResult(dto.getPage()
            ,dto.getSize()
            ,(int)page.getTotal());

    result.setData(page.getRecords());
    return result;

}


    @Override
    public ResponseResult findNewsById(Integer id) {
        WmNews wmNews = this.newsMapper.selectById(id);
        return ResponseResult.okResult(wmNews);
    }
    //人工审核失败  驳回

    @Override
    public ResponseResult authFail(NewsAuthDto dto) {
        WmNews wmNews = this.newsMapper.selectById(dto.getId());
        if (wmNews.getStatus()==3){
            wmNews.setStatus((short) 2);
            wmNews.setReason(dto.getMsg());
            this.newsMapper.updateById(wmNews);
        }
        return ResponseResult.errorResult(500,"审核失败"+dto.getMsg());
    }


    @Resource
    private WmNewsTaskService wmNewsTaskService;
    //人工审核成功   通过
    @Override
    public ResponseResult authPass(NewsAuthDto dto) {
        WmNews wmNews = this.newsMapper.selectById(dto.getId());
        if (wmNews.getStatus()==3){
            wmNews.setStatus((short) 4);
            wmNews.setReason("人工审核通过");
            this.newsMapper.updateById(wmNews);
            //将审核通过的新闻添加到任务中，以便进行下一步处理。
            this.wmNewsTaskService.addNewsToTask(wmNews);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
